import java.awt.*; import java.applet.*; import java.awt.event.*; import java.awt.geom.*; public class DragAndDropApplet extends Applet implements MouseListener, MouseMotionListener { //============================================================================================= // Data fields used in the program. //============================================================================================= public boolean dragging; public double squareX; public double squareY; //============================================================================================= // This method takes care of setting the listeners and initializes the datafields. //============================================================================================= public void init() { setBackground(Color.green); addMouseListener(this); //listen for mouse events addMouseMotionListener(this); //listen for mouse motion events squareX = 100; squareY = 100; dragging = false; } //============================================================================================= // This method simply paints the square at the squareX and squareY values. These values are // updated in the mouseMoved method. //============================================================================================= public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; Rectangle2D.Double r = new Rectangle2D.Double(squareX-25, squareY-25, 50, 50); g2D.fill(r); } //============================================================================================= // This method will relocate the center of the square if the square is currently being dragged. // It will then call the repaint method so that the square is actually painted at the new location. //============================================================================================= public void mouseDragged(MouseEvent e) { if(dragging == true) { squareX = e.getX(); //set squareX to mouse's X squareY = e.getY(); //set squareY to mouse's Y repaint(); } } //============================================================================================= // This method simply sets the 'dragging' variable to true if the click is on the square. //============================================================================================= public void mousePressed(MouseEvent e) { showStatus(e.getX() + "," + e.getY()); if (e.getX() > squareX-25 && e.getX() < squareX+25 && e.getY() > squareY-25 && e.getY() < squareY+25) //if click is on square { dragging = true; showStatus("YES B" + e.getX() + "," + e.getY()); } } //============================================================================================= // This method simply set the 'dragging' variable to false. //============================================================================================= public void mouseReleased(MouseEvent e) { showStatus("released"); dragging = false; } //============================================================================================= // The following are the interface (MouseListener and MouseMotionListener) methods that we will // not be using in this program. //============================================================================================= public void mouseClicked(MouseEvent e) { //BLANK - NOT USED HERE } public void mouseEntered(MouseEvent e) { //BLANK - NOT USED HERE } public void mouseExited (MouseEvent e) { //BLANK - NOT USED HERE } public void mouseMoved(MouseEvent e) { //BLANK - NOT USED HERE } }